# CMake 最低版本要求
# 这里设置为 3.10 以支持 CUDA 和 C++17
cmake_minimum_required(VERSION 3.10)

# 定义项目名称 支持语言
project(xmatrix LANGUAGES CXX CUDA)

# 包含头文件目录
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)

# 收集 src 中的源文件
file(GLOB_RECURSE SOURCES
    "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp"
    "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cu"
    "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cuh"
)

# 创建 CUDA 库
add_library(${PROJECT_NAME} ${SOURCES})


# 检查 clang-format 是否可用
find_program(CLANG_FORMAT "clang-format")
# if(NOT CLANG_FORMAT)
    # message(WARNING "clang-format not found. May install it to enable source code formatting.")
# else()
if(CLANG_FORMAT)
    # 收集 include 中的头文件（自动查找 .h .hpp）
    file(GLOB_RECURSE HEADERS
        "${CMAKE_CURRENT_SOURCE_DIR}/include/*.h"
        "${CMAKE_CURRENT_SOURCE_DIR}/include/*.hpp"
    )
    # 合并所有需要格式化的文件
    set(ALL_SOURCES ${SOURCES} ${HEADERS})
    add_custom_target(format_${PROJECT_NAME}
        COMMAND ${CLANG_FORMAT} -i ${ALL_SOURCES} 
        COMMENT "Formatting ${ALL_SOURCES}"
        WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
        VERBATIM
    )
    add_dependencies(${PROJECT_NAME} format_${PROJECT_NAME})
endif()

# 设置 C++17 标准
target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_17)

# 设置 CUDA 架构
set_property(TARGET ${PROJECT_NAME} PROPERTY CUDA_ARCHITECTURES 60 75 86)

# 导出头文件给使用者
target_include_directories(${PROJECT_NAME}
    PUBLIC
        $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
        $<INSTALL_INTERFACE:include>
)